package com.houger.ddd.common.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * @author Houger.
 * @date 2024年05月30日 14:48.
 */
public class DateUtils {
    public static long localDateTimeToMillis(LocalDateTime localDateTime) {
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return instant.toEpochMilli();
    }


    public static String getNowFormat(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return now.format(formatter);
    }

    public static LocalDateTime toLocalDateTime(final Integer seconds) {
        return LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds), ZoneId.systemDefault());
    }

    public static Long getTimeDifference(LocalDateTime startTime, final LocalDateTime endTime) {
        if (Objects.isNull(startTime)){
            startTime=LocalDateTime.now();
        }
        if (startTime.isAfter(endTime)){
            throw new BusinessException(StatusCode.START_THAN_END,null);
        }

        return localDateTimeToMillis(endTime)-localDateTimeToMillis(startTime);
    }
}
